package com.lh_travel.lohas.activity.user;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.TextView;

import com.lh_travel.lohas.R;
import com.lh_travel.lohas.base.api;
import com.lh_travel.lohas.base.baseActivity;
import com.lh_travel.lohas.domain.Feedback;
import com.lh_travel.lohas.domain.UserBean;
import com.lh_travel.lohas.utils.ImageUtils;
import com.lh_travel.lohas.utils.PhotoUtil;
import com.lh_travel.lohas.utils.Preferences;
import com.tamic.novate.Throwable;
import com.tamic.novate.callback.RxResultCallback;

public class FeedbackActivity extends baseActivity {

    private Toolbar toolbar;
    private ImageView imageAdd;
    private String imgAddress;
    private Bitmap bit;
    private MyThread myThread;
    private RadioButton rb_1;
    private RadioButton rb_2;
    private RadioButton rb_3;
    private RadioButton rb_4;
    private String type = "1";
    private EditText et_content;
    private TextView tv_submit;
    private boolean submitPic;
    private UserBean userBean;
    private ImageButton img_back;

    @SuppressLint("HandlerLeak")
    private Handler uiHandle = new Handler() {
        @Override
        public void handleMessage(Message msg) {

            switch (msg.what) {

                case 1:

                    submitPic = true;

                    String path = (String) msg.obj;

                    imageAdd.setImageBitmap(BitmapFactory.decodeFile(path));

                    new api(mContext).feedBackImage(path, new RxResultCallback<Feedback.norBean>() {
                        @Override
                        public void onNext(Object tag, int code, String message, Feedback.norBean response) {

                            if (code == 200){
                                imgAddress = response.image;
                            }
                        }

                        @Override
                        public void onError(Object tag, Throwable e) {

                        }

                        @Override
                        public void onCancel(Object tag, Throwable e) {

                        }
                    });

                    break;

            }

        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_feedback);

        findView();
        bindListner();
        ensureUI();
    }

    @Override
    public void findView() {

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        imageAdd = (ImageView) findViewById(R.id.imageAdd);
        et_content = (EditText) findViewById(R.id.et_content);
        tv_submit = (TextView) findViewById(R.id.tv_submit);
        img_back = (ImageButton) findViewById(R.id.img_back);

        rb_1 = (RadioButton) findViewById(R.id.rb_1);
        rb_2 = (RadioButton) findViewById(R.id.rb_2);
        rb_3 = (RadioButton) findViewById(R.id.rb_3);
        rb_4 = (RadioButton) findViewById(R.id.rb_4);
    }

    @Override
    public void bindListner() {

        img_back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });

        imageAdd.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                PhotoUtil.selectPictureFromAlbum(FeedbackActivity.this);
            }
        });

        rb_1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked){
                    type = "1";
                }

            }
        });

        rb_2.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked){
                    type = "2";
                }

            }
        });

        rb_3.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked){
                    type = "3";
                }

            }
        });

        rb_4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked){
                    type = "4";
                }

            }
        });

        tv_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                if (submitPic && imgAddress == null){
                    showMessage("反馈图片正在上传");

                }else {
                    new api(mContext).feedback(userBean.token, type, et_content.getText().toString().trim(), imgAddress, new RxResultCallback<Object>() {
                        @Override
                        public void onError(Object tag, Throwable e) {

                        }

                        @Override
                        public void onCancel(Object tag, Throwable e) {

                        }

                        @Override
                        public void onNext(Object tag, int code, String message, Object response) {

                            if (code == 200){
                                showMessage("反馈成功");
                                finish();
                            }else {

                                showMessage("反馈失败");
                            }

                        }
                    });
                }
            }
        });

    }

    @Override
    public void ensureUI() {

        setSupportActionBar(toolbar);
        toolbar.setEnabled(true);

        userBean = mApp.getBeanPrefernce(Preferences.LOCAL.USERLOGIN);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        switch (requestCode) {
            // 读取相册缩放图片
            case PhotoUtil.PHOTOZOOM:

                try {

                    Uri uri = data.getData();
                    bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(uri));

                    myThread = new MyThread(bit, uiHandle);

                    myThread.start();

                } catch (Exception e) {
                    e.printStackTrace();
                }

                break;
        }
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        if (myThread != null){

            bit = null;
            myThread.close();
        }

        uiHandle.removeCallbacksAndMessages(null);
    }

    private static class MyThread extends Thread {

        private boolean mRunning = false;
        private Bitmap bitmap;
        private Handler uiHandle;

        public MyThread( Bitmap bit, Handler handler) {
            this.bitmap = bit;
            this.uiHandle = handler;
        }

        @Override
        public void run() {
            mRunning = true;

            String bitmap = ImageUtils.compressImage(this.bitmap, "lohasfeedback", 100);

            Message msg = uiHandle.obtainMessage();

            msg.what = 1;

            msg.obj = bitmap;

            uiHandle.sendMessage(msg);
        }

        public void close() {
            mRunning = false;
        }
    }
}
